

local storyboard = require("storyboard")
local scene = storyboard.newScene()

local widget = require("widget")

local group,
	q, 				--the state to be asked
	count,			--the count of the questions 
	states,			--the states and its capital etc
	queries,		-- the type of question to be asked
	key,keyP,		--the key and the key position
	round,
	question,
	instruction, button1, button2, button3,
	scoreText, leveltext, roundtext, title
	
local button = {} 
local labels = {}
	

local function moveon()
	local i
	if (gameLevel == 1) then
		q = 2 --the capital will be asked
	elseif (gameLevel == 2) then
		q = math.random(3,4)	--choice between bird or tree
	else
		q = math.random(2,5)	--choice between capital,bird,tree and flower 
	end
	keyP = math.random(1,3)
	key = math.random(1,50)
	for i = 1, 3, 1
		if(i == keyP) then
			label = states[key][q]
			print(label)
		else
			opt = math.random(1,50)						
			label = states[opt][2]
			print(label)
		end
	end
	
end

function scene:createScene(event)
	
	function showme()
	group = display.newGroup()
	
	background = display.newImageRect( "images/bg22.jpg",  display.contentWidth,  display.contentHeight)
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
	
	group:insert(background)
	group:setReferencePoint( display.CenterReferencePoint )
	
	gameLevel = 1
	queries = {'name','capital','bird','tree','flower','abbreviation'}
	-- {name, capital, bird, tree, flower}
	states = {
		{ "Alabama" , "Montgomery", "Yellowhammer", "Longleaf Pine", "Camelia", "AL" },
		{ "Alaska" , "Juneau", "Willow Ptargmigan", "Sitkaspruce", "Forget Me Not", "AK" },
		{ "Arizona" , "Phoenix", "Cactus Wren", "Yellow Palo Verde", "Saguaro cactus Blossom", "AZ" },
		{ "Arkansas" , "Little Rock", "Mokingbird", "Shortleaf Pine", "Appleblossom", "AR" },
		{ "California" , "Sacramento", "California Valley Quail", "Redwood", "California Poppy", "CA" },
		{ "Colorado" , "Denver", "Lark Bunting", "Blue Spruce", "Rocky Mountain Columbine", "CO" },
		{ "Connecticut" , "Hartford", "Robin", "White Oak", "Mountain Laurel", "CT" },
		{ "Delaware" , "Dover", "Blue Hen Chicken", "American Holly", "Peach Blossom", "DE" },
		{ "Florida" , "Tellahassee", "Mockingbird", "Cabbage Palmetto", "Orange Blossom", "FL" },
		{ "Georgia" , "Atlanta", "Brown Thrasher", "Live oak", "Cherokee Rose", "GA" },
		{ "Hawaii" , "Honolulu", "Nene", "Kukui-Candlenut", "Hibiscus", "HI" },
		{ "Idaho" , "Boise", "Mountain Bluebird", "Western Whitepine", "Syringa", "ID" },
		{ "Illinois" , "Springfield", "Cardinal", "White Oak", "Purple Violet", "IL" },
		{ "Indiana" , "Indianapolis", "Cardinal", "Yellow Poplar", "Peony", "IN" },
		{ "Iowa" , "Des Moines", "Eastern Goldfinch", "Oak", "Wild Prairie Rose", "IA" },
		{ "Kansas" , "Topeka", "Western Meadowlark", "Cottonwood", "Sunflower", "KS" },
		{ "Kentucky" , "Frankfort", "Cardinal", "Yellow Poplar", "Goldenrod", "KY" },
		{ "Louisiana" , "Baton Rouge", "Eastern Brown Pelican", "Bald Cypress", "Magnolia", "LA" },
		{ "Maine" , "Augusta", "Chickadee", "Eastern White Pine", "White Pinecone and Tassel", "ME" },
		{ "Maryland" , "Annapolis", "Baltimore Oriole", "White Oak", "Black-eyed Susan", "MD" },
		{ "Massachusetts" , "Boston", "Chickadee", "American Elm", "Mayflower", "MA" },
		{ "Michigan" , "Lansing", "Robin", "Eastern White Pine", "Apple Blossom", "MI" },
		{ "Minnesota" , "Saint Paul", "Common Loon", "Red Pine", "Pink and White Lady's Slippers", "MN" },
		{ "Mississippi" , "Jackson", "Mockingbird", "Southern Magnolia", "Magnolia", "MS" },
		{ "Missouri" , "Jefferson City", "Bluebird", "Flowering Dogwood", "Hawthorn", "MO" },
		{ "Montana" , "Helena", "Western Meadowlark", "Ponderosa Pine", "Bitterroot", "MT" },
		{ "Nebraska" , "Lincoln", "Western Meadowlark", "Cottonwood", "Goldernrod", "NE" },
		{ "Nevada" , "Carson City", "Mountain Bluebird", "Bristlecone Pine", "Sagebrush", "NV" },
		{ "New Hampshire" , "Concord", "Purple Finch", "Paper Birch", "Purple Lilac", "NH" },
		{ "New Jersey" , "Trenton", "Eastern Goldfinch", "Northern Red Oak", "Violet", "NJ" },
		{ "New Mexico" , "Santa Fe", "Roadrunner", "Pinyon Pine", "Yucca Flower", "NM" },
		{ "New York" , "Albany", "Bluebird", "Sugar Maple", "Rose", "NY" },
		{ "North Carolina" , "Raleigh", "Cardinal", "Longleaf Pine", "Dogwood", "NC" },
		{ "North Dakota" , "Bismarck", "Western Meadowlark", "American Elm", "Wild Prairie Rose", "ND" },
		{ "Ohio" , "Columbus", "Cardinal", "Ohio Buckeye", "Scarlet Carnation", "OH" },
		{ "Oklahoma" , "Oklahoma City", "Scissor-tailed Flycatcher", "Redbud", "Mistletoe", "OK" },
		{ "Oregon" , "Salem", "Western Meadowlark", "Douglas Fir", "Oregon Grape", "OR" },
		{ "Pennsylvania" , "Harrisburg", "Ruffed Grouse", "Eastern Hemlock", "Mountain Laurel", "PA" },
		{ "Rhode Island" , "Providence", "Rhode Island Red", "Red Mapple", "Violet", "RI" },
		{ "South Carolina" , "Columbia", "Great Carolina Wren", "Cabbage Palmetto", "Yellow Jessamine", "SC" },
		{ "South Dakota" , "Pierre", "Ring-necked Pheasant", "White Spruce", "Pasque Flower", "SD" },
		{ "Tennessee" , "Nashville", "Mockingbird", "Yellow Poplar", "Iris", "TN" },
		{ "Texas" , "Austin", "Mockingbird", "Pecan", "Bluebonnet", "TX" },
		{ "Utah" , "Salt Lake City", "Common American Gull", "Blue Spruce", "Sego Lily", "UT" },
		{ "Vermont" , "Montpelier", "Hermit Thrush", "Sugar Maple", "Red Clover", "VT" },
		{ "Virginia" , "Richmond", "Cardinal", "Flowering Dogwood", "Dogwood", "VA" },
		{ "Washington" , "Olympia", "Willow Goldfinch", "Western Hemlock", "Pink Rhododendron", "WA" },
		{ "West Virginia" , "Charleston", "Cardinal", "Sugar Maple", "Rhododendron", "WV" },
		{ "Wisconsin" , "Madison", "Robin", "Sugar Maple", "Wood Violet", "WI" },
		{ "Wyoming" , "Cheyenne", "Western Meadowlark", "Cottonwood", "Indian Paintbrush", "WY" },
	}
	
	title = display.newText("US States", 0,0, font, 18)
	title:setReferencePoint(display.CenterReferencePoint)
	title .x = display.contentWidth * .5
	title .y = display.contentHeight * .15
	title:setTextColor(96, 17, 25)
	group:insert(title)
	
	instruction = display.newText("", 0, 0, font, 24)
	instruction:setReferencePoint(display.CenterReferencePoint)
	instruction.x = display.contentWidth * .5
	instruction.y = display.contentHeight * .30
	instruction:setTextColor(5, 61, 32)
	group:insert(instruction)
	
	--the question tada
	question = display.newText("", 0, 0, native.systemFont, 32)
	question.x = display.contentWidth * 0.5
	question.y = display.contentHeight * 0.35
	question:setTextColor(96, 17, 25)
	question:setReferencePoint(display.CenterReferencePoint)
	group:insert(question)
		
	scoreText = display.newText("Score: "..0, 0,0, font, 18)
	scoreText.x = display.contentWidth * .8
	scoreText.y = display.contentHeight * .25
	scoreText:setTextColor(96, 17, 25)
	group:insert(scoreText)
	
	roundtext = display.newText("Round: "..0, 0,0, font, 18)
	roundtext.x = display.contentWidth * .8
	roundtext.y = display.contentHeight * .2
	roundtext:setTextColor(96, 17, 25)
	group:insert(roundtext)
	
	leveltext = display.newText("Level: "..gameLevel, 0,0, font, 18)
	leveltext.x = display.contentWidth * .2
	leveltext.y = display.contentHeight * .2
	leveltext:setTextColor(96, 17, 25)
	group:insert(leveltext)		
	--get the values
	end
end

function scene:enterScene(event)
	storyboard.purgeScene( "first" )
	score = 0
	count = 5
	round = 0
	level = 1
	showme()
	moveon()
	--the function to catch the student's answer
	local answerMe = function(event)
		local imageId
		imageId = event.target.id
		if(imageId == keyP) then
			score = score + 100
		else
			score = score - 10
			round = round - 1
		end
		for i = 1, 3 do
			group:remove(button[i])
		end
		moveon()
		
		timer.performWithDelay(500, start , 1)
	end
	function start()
		round = round + 1
		scoreText.text = "Score: "..score
		if(round <= count) then
			instruction.text = "What is the "..queries[q].." of"
			question.text = "" .. states[key][1]
			roundtext.text = "Round: "..round
			print("Ans: "..states[key][2].." "..keyP)
			local i, opt, label, spos
			spos = .45
			for i = 1, 3, 1 do
				button[i] = widget.newButton{
					label =""..labels[i],fontSize = 25,labelColor = {default={255,255,255,255},over={255,255,255,255}},
					emboss = false,defaultColor = {60,79,60,255},overColor = {108,142,108,255},width = 250,height = 30,
					strokeColor = {108,142,108,255},cornerRadius = 2, id=i
				}
				button[i].x = display.contentWidth * 0.5; button[i].y = display.contentHeight * spos
				button[i]:addEventListener("tap", answerMe)
				group:insert(button[i])
				spos = spos + 0.06
			end
		else
			timer.performWithDelay(1000,  function() group:removeSelf() storyboard.gotoScene( "first", "zoomOutInFade", 500 ) end , 1)
		end
	end
	
	start()
end

function scene:exitScene(event)

end

function scene:destroyScene(event)

end

scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene